﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;

public class LoadScene_Async : MonoBehaviour
{
    //显示进度的文本
    [SerializeField]private TextMeshProUGUI progressText;
    //进度条
    [SerializeField] private Slider slider;
    [SerializeField] private GameObject overMaskCanvas;

    //进度条的数值
    private float progressValue;
    

    [Tooltip("下个场景的名字")]
    public static string nextSceneName;

    private AsyncOperation async = null;

    private void Start()
    {
       
        StartCoroutine(LoadScene());
        DontDestroyOnLoad(overMaskCanvas);
    }

    IEnumerator LoadScene()
    {
        async = SceneManager.LoadSceneAsync(nextSceneName);
        async.allowSceneActivation = false;
        bool isDone=false;
        while (!isDone)
        {
            progressValue = async.progress;
            isDone = async.isDone;
            slider.value = progressValue+0.1f;
            progressText.text = (int)(slider.value * 100) + " %";

            if (progressValue >= 0.9f)
            {
                progressText.text = "按任意键继续";
                overMaskCanvas.SetActive(true);

                async.allowSceneActivation = true;

            }
            Debug.Log("isDone " + isDone);
            yield return null;
        }
    }

    public static void LoadScene(string nextSceneName)
    {
        LoadScene_Async.nextSceneName = nextSceneName;
        SceneManager.LoadScene("LoadingScene");
    }
}
